package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zy4/api/request"
	"zy4/internal/dao"
	"zy4/pkg"
)

func Login(c *gin.Context) {
	var req request.LoginUserReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	user := dao.GetUserMobile(req.Mobile)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}

	user = dao.GetUserMobileAndPassword(req.Mobile, user.Password)
	token, _ := pkg.NewJWT("2210a").CreateToken(pkg.CustomClaims{
		ID: uint(user.Id),
	})

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": token,
	})
}
